package com.uwlaser.rest;

import com.uwlaser.annotation.Log;
import com.uwlaser.service.ProductMaterialService;
import com.uwlaser.service.dto.ProductMaterialDto;
import com.uwlaser.service.dto.ProductMaterialQueryParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

/**
* @author zk-Qin
* @date 2022-05-11
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "物料管理管理")
@RequestMapping("/api/productMaterial")
public class ProductMaterialController {

    private final ProductMaterialService productMaterialService;

    @GetMapping
    @Log("查询物料管理")
    @ApiOperation("查询物料管理")
    @PreAuthorize("@el.check('productMaterial:list')")
    public ResponseEntity query(ProductMaterialQueryParam query, Pageable pageable){
        return new ResponseEntity<>(productMaterialService.queryAll(query,pageable),HttpStatus.OK);
    }

    @GetMapping("/list")
    @Log("查询物料列表")
    @ApiOperation("查询物料列表")
    @PreAuthorize("@el.check('productMaterial:list')")
    public ResponseEntity list(ProductMaterialQueryParam query){
        return new ResponseEntity<>(productMaterialService.queryAll(query),HttpStatus.OK);
    }

    @GetMapping("/notBindList")
    @Log("查询未被绑定的物料列表")
    @ApiOperation("查询未被绑定的物料列表")
    @PreAuthorize("@el.check('productMaterial:list')")
    public ResponseEntity notBindList(ProductMaterialDto query){
        return new ResponseEntity<>(productMaterialService.queryNotBindList(query),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增物料管理")
    @ApiOperation("新增物料管理")
    @PreAuthorize("@el.check('productMaterial:add')")
    public ResponseEntity create(@Validated @RequestBody ProductMaterialDto resources){
        return new ResponseEntity<>(productMaterialService.insert(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改物料管理")
    @ApiOperation("修改物料管理")
    @PreAuthorize("@el.check('productMaterial:edit')")
    public ResponseEntity update(@Validated @RequestBody ProductMaterialDto resources){
        productMaterialService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除物料管理")
    @ApiOperation("删除物料管理")
    @PreAuthorize("@el.check('productMaterial:del')")
    public ResponseEntity delete(@RequestBody Set<Integer> ids) {
        productMaterialService.removeByIds(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /*
    @Log("导出物料管理")
    @ApiOperation("导出物料管理")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('productMaterial:list')")
    public void download(HttpServletResponse response, ProductMaterialQueryParam query) throws IOException {
        productMaterialService.download(productMaterialService.queryAll(query), response);
    }*/

}
